package org.samples.service.imp;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.samples.dao.IExamTestDAO;
import org.samples.dao.IExamTestTypeDAO;
import org.samples.dao.IExamUserDAO;
import org.apache.poi.ss.usermodel.Workbook;
import org.samples.dao.IExamScoreDAO;
import org.samples.dao.IExamSubjectDAO;
import org.samples.model.ExamTest;
import org.samples.model.ExamTestType;
import org.samples.model.ExamUser;
import org.samples.model.PagerModel;
import org.samples.model.ExamSubject;
import org.samples.model.vo.ExamTestTypeVO;
import org.samples.model.vo.ExamTestVO;
import org.samples.model.vo.ExamUserVO;
import org.samples.model.vo.ExamScoreVO;
import org.samples.model.vo.ExamSubjectVO;
import org.samples.service.ITeacherService;
import org.samples.util.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TeacherServiceImp implements ITeacherService {
	
	@Autowired
	private IExamTestDAO examTestDAO;
	
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	
	@Autowired
	private IExamUserDAO examUserDAO;
	
	@Autowired
	private IExamScoreDAO examScorerDAO;

	//-------------------------------------------I am the dividing line-------------------------------------------
	//subject
	
	@Override
	public List<ExamSubject> findAllSubject() {
		return examSubjectDAO.find("From ExamSubject");
	}

	@Override
	public ExamSubject getSubjectById(Integer subId) {
		return examSubjectDAO.get(subId);
	}

	@Override
	public void editSubject(ExamSubject subject) throws Exception {
		examSubjectDAO.update(subject);
	}

	@Override
	public void creatSubject(ExamSubject subject) throws Exception {
		examSubjectDAO.save(subject);
	}

	@Override
	public void deleteSubject(Integer subId) throws Exception {
		examSubjectDAO.delete(examSubjectDAO.get(subId));
	}

	@Override
	public List<String> getAllUserBySubject(Integer subId) {
		return examSubjectDAO.getAllUserBySubject(subId);
	}

	@Override
	public void saveSubjectStudent(List<String> subjectStudents, Integer subId) throws Exception {
		List<String> dbSubjectUserList = getAllUserBySubject(subId);
		List<String> insertList = new ArrayList<String>();
		List<String> delList = new ArrayList<String>();
		
		for(String selectedUser : subjectStudents){
			if(!dbSubjectUserList.contains(selectedUser)){
				insertList.add(selectedUser);
			}
		}
		
		for(String selectedUser : dbSubjectUserList){
			if(!subjectStudents.contains(selectedUser)){
				delList.add(selectedUser);
			}
		}
		
		examSubjectDAO.insertSubjectUsers(insertList, subId);
		examSubjectDAO.deleteSubjectUsers(delList, subId);
	}

	@Override
	public PagerModel<ExamSubject> findSubjectPage() {
		PagerModel<ExamSubject> pageList =  examSubjectDAO.findAll();
		pageList.setOffset(pageList.getOffset());
		pageList.setPageSize(pageList.getPageSize());
		pageList.setTotalSize(pageList.getTotalSize());
		return pageList;
	}
	
	//subject
	//-------------------------------------------I am the dividing line-------------------------------------------
	//test
	
	@Override
	public PagerModel<ExamTestVO> findAllExamTest() {
		PagerModel<ExamTest> examTestList =  examTestDAO.findAllExamTest();
		if(examTestList != null){
			PagerModel<ExamTestVO> pageList = new PagerModel<ExamTestVO>();
			pageList.setDates(new ArrayList<ExamTestVO>());
			for(ExamTest entity : examTestList.getDates()){
				ExamTestVO vo = new ExamTestVO();
				ExamTestTypeVO typeVO = new ExamTestTypeVO();
				BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
				BeanUtils.copyProperties(entity, vo);
				vo.setExamTestTypeVO(typeVO);
				ExamSubject subject = examSubjectDAO.get(entity.getSubId());
				if(subject != null) vo.setName(subject.getName());
				pageList.getDates().add(vo);
			}
			pageList.setOffset(examTestList.getOffset());
			pageList.setPageSize(examTestList.getPageSize());
			pageList.setTotalSize(examTestList.getTotalSize());
			return pageList;
		}
		return null;
	}

	@Override
	public ExamTestVO getExamTestById(Integer id) {
		ExamTest entity =  examTestDAO.get(id);
		if(entity != null){
			ExamTestVO vo = new ExamTestVO();
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(entity, vo);
			BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
			vo.setExamTestTypeVO(typeVO);
			ExamSubject subject = examSubjectDAO.get(entity.getSubId());
			if(subject != null) vo.setName(subject.getName());
			return vo;
		}
		return null;
	}

	@Override
	public void editExamTest(ExamTestVO examTestVO) throws Exception {
		ExamTest examTest = new ExamTest();
		ExamTestType examTestType = examTestTypeDAO.get(examTestVO.getExamTestTypeVO().getId());
		BeanUtils.copyProperties(examTestVO, examTest);
		examTest.setExamTestType(examTestType);
		examTestDAO.update(examTest);;
	}

	@Override
	public void createExamTest(ExamTestVO examTestVO) throws Exception {
		ExamTest examTest = new ExamTest();
		ExamTestType examTestType = examTestTypeDAO.get(examTestVO.getExamTestTypeVO().getId());
		BeanUtils.copyProperties(examTestVO, examTest);
		examTest.setId(1);
		examTest.setExamTestType(examTestType);
		examTestDAO.save(examTest);
	}

	@Override
	public void deleteExamTest(Integer id) throws Exception {
		examTestDAO.delete(examTestDAO.get(id));;
	}

	@Override
	public List<ExamSubjectVO> getSubjectList() throws Exception {
		List<ExamSubjectVO> subjectVOList = new ArrayList<ExamSubjectVO>();
		List<ExamSubject> subjectList = examSubjectDAO.find("From ExamSubject");
		for(ExamSubject subject : subjectList){
			ExamSubjectVO vo = new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			subjectVOList.add(vo);
		}
		return subjectVOList;
	}
	
	@Override
	public List<ExamTestTypeVO> getTestTypeList() throws Exception {
		List<ExamTestTypeVO> testTypeVOList = new ArrayList<ExamTestTypeVO>();
		List<ExamTestType> testTypeList = examTestTypeDAO.find("from ExamTestType");
		for(ExamTestType testType : testTypeList){
			ExamTestTypeVO vo = new ExamTestTypeVO();
			BeanUtils.copyProperties(testType, vo);
			testTypeVOList.add(vo);
		}
		return testTypeVOList;
	}

	//test
	//-------------------------------------------I am the dividing line-------------------------------------------
	//student
	
	@Override
	public List<ExamUser> findAllUser() {
		return examUserDAO.find("FROM ExamUser");
	}
	
	@Override
	public PagerModel<ExamUserVO> findUserPage() {
		PagerModel<ExamUser> list = examUserDAO.findUserPage();
		if(list != null){
			PagerModel<ExamUserVO> pageList = new PagerModel<ExamUserVO>();
			pageList.setDates(new ArrayList<ExamUserVO>());
			for(ExamUser user : list.getDates()){
				ExamUserVO vo = new ExamUserVO();
				BeanUtils.copyProperties(user, vo);
				if(user.getGender() == 0) vo.setGender("男");
				if(user.getGender() == 1) vo.setGender("女");
				pageList.getDates().add(vo);
			}
			pageList.setOffset(list.getOffset());
			pageList.setPageSize(list.getPageSize());
			pageList.setTotalSize(list.getTotalSize());
			return pageList;
		}
		
		return null;
	}

	@Override
	public ExamUser getUserById(String userId) {
		return examUserDAO.getById(userId);
	}

	@Override
	public ExamUser getByUserName(String userName) {
		return (ExamUser) examUserDAO.createQuery("From ExamUser Where userName = ?").setParameter(0, userName).uniqueResult();
	}

	@Override
	public void editUser(ExamUser user) throws Exception {
		examUserDAO.editUser(user);
	}

	@Override
	public void creatUser(ExamUser user) throws Exception {
		user.setLoginFailCount(0);;
		examUserDAO.creatUser(user);
	}

	@Override
	public void deleteUser(String userId) throws Exception {
		examUserDAO.deleteUser(userId);
	}

	@Override
	public void saveUserByExcel(InputStream inputStream, String postfix) throws IOException {
		Workbook workbook = ExcelUtils.openWorkBook(inputStream, postfix);
		List<ExamUser> entities = ExcelUtils.getUserListByExcel(workbook);
		examUserDAO.save(entities);
	}

	//student
	//-------------------------------------------I am the dividing line-------------------------------------------
	//score
	
	@Override
	public PagerModel<ExamScoreVO> getAllScore(Map<String, String> conditionMap) {
		return examScorerDAO.getAllScore(conditionMap);
	}

}
